from org.kwisatz.proxybot import *
class Whois:
	def __init(self):
		self.channels=[]
		self.modes=''
		self.nick=''
		self.user=''
		self.vhost=''
		self.real_name=''
	def __str__(self):
		return ( str(self.channels)+' '+
				self.modes+' '+
				self.nick+' '+
				self.user+' '+
				self.vhost+' '+
				self.real_name+' ')

class Filter(FilterAdapter):
	def __init__(self):
		self.callback = None
		self.whois = Whois()
	def handleServerLine(self, line, msg):
		if self.callback == None:
			return line
		if msg.command == '319':
			self.whois.channels = msg.endOfMsg.split()
		elif msg.command == '379':
			self.whois.modes = msg.endOfMsg[16:].strip()
		elif msg.command == '311':
			cmd=msg.cmdParam.split()
			self.whois.nick = cmd[1]
			self.whois.user = cmd[2]
			self.whois.vhost = cmd[3]
			self.whois.real_name = msg.endOfMsg
		elif msg.command == '318':
			self.callback(self.whois)
			self.callback=None
		return line
	def query(self, nick, callback):
		self.context.server.println('WHOIS '+nick)
		self.callback=callback
	def getID(self):
		return 'whois'

